﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Entity.OrganizationManage;
using YiSha.Model.Param.OrganizationManage;
using YiSha.Service.OrganizationManage;

namespace YiSha.Business.OrganizationManage
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2023-04-11 22:01
    /// 描 述：租户表业务类
    /// </summary>
    public class SysTenantBLL
    {
        private SysTenantService sysTenantService = new SysTenantService();
        private UserService userService = new UserService();
        #region 获取数据
        public async Task<TData<List<SysTenantEntity>>> GetList(SysTenantListParam param)
        {
            TData<List<SysTenantEntity>> obj = new TData<List<SysTenantEntity>>();
            obj.Data = await sysTenantService.GetList(param);
            obj.Total = obj.Data.Count;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<List<SysTenantEntity>>> GetPageList(SysTenantListParam param, Pagination pagination)
        {
            TData<List<SysTenantEntity>> obj = new TData<List<SysTenantEntity>>();
            obj.Data = await sysTenantService.GetPageList(param, pagination);
            obj.Total = pagination.TotalCount;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<SysTenantEntity>> GetEntity(long id)
        {
            TData<SysTenantEntity> obj = new TData<SysTenantEntity>();
            obj.Data = await sysTenantService.GetEntity(id);
            if (obj.Data != null)
            {
                obj.Tag = 1;
            }
            return obj;
        }
        #endregion

        #region 提交数据
        public async Task<TData<string>> SaveForm(SysTenantEntity entity)
        {
            TData<string> obj = new TData<string>();

            //判断数据是否存在
            //if (sysTenantService.GetIsExist(t=>t.Id!=entity.Id && t.contact_user_id==entity.contact_user_id).Result)
            //{
            //    obj.Message = "租户已存在";
            //    obj.Tag =0;
            //    return obj;
            //}
            await sysTenantService.SaveForm(entity);

            //同时更新用户表
            //var user=userService.GetEntity(entity.contact_user_id.Value).Result;

            //if (!user.IsEmpty())
            //{
            //    user.tenant_id = entity.Id;
            //    await userService.SaveForm(user);
            //}

            obj.Data = entity.Id.ParseToString();
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData> DeleteForm(string ids)
        {
            TData obj = new TData();
            await sysTenantService.DeleteForm(ids);
            obj.Tag = 1;
            return obj;
        }
        #endregion

        #region 私有方法
        #endregion
    }
}
